const Sequelize = require('sequelize');
const mysqlConfig = require('./Mysql');
const cls = require('continuation-local-storage');
const { timeGetter } = require('../utils/ModelUtil');

// Default options for model definitions
Object.assign(mysqlConfig, {
  define: {
    version: true, // Enable optimistic locking
    getterMethods: {
      createdAt: timeGetter(),
      updatedAt: timeGetter(),
    },
  },
});

const namespace = cls.createNamespace('transaction');
Sequelize.useCLS(namespace);

const db = new Sequelize(mysqlConfig);

module.exports = db;
